#!/bin/bash

# templatex-dependencies.sh is a script for TempLaTeX.
#
# TempLaTeX depends on several packages that are usually not installed by
# default. LaTeX editors like MikTeX or Kile can retrieve dependencies
# when required, but this is not possible when using Emacs+AucTeX. This
# bash script searches for non-installed dependencies. Then it retrieves
# them from CTAN, and compiles and installs them.
# 
# Author: Ramon Casero <rcasero@gmail.com>
#                                                                              
# This library is free software; you can redistribute it and/or                
# modify it under the terms of the GNU Lesser General Public                   
# License as published by the Free Software Foundation; either                 
# version 2.1 of the License, or (at your option) any later version.           
#                                                                              
# This library is distributed in the hope that it will be useful,              
# but WITHOUT ANY WARRANTY; without even the implied warranty of               
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU            
# Lesser General Public License for more details.                              
#                                                                              
# You should have received a copy of the GNU Lesser General Public             
# License along with this library; if not, write to the Free Software          
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA 

# create temp directory to dowload files
TMPDIR=`mktemp -d`
pushd $TMPDIR > /dev/null

# teTex package root path
TEXMFLOCAL=`kpsewhich --var-value TEXMFLOCAL`
PKGLOCAL=${TEXMFLOCAL}/tex/latex

# CTAN package URLs
CTAN_TEXARCH=ftp://tug.ctan.org/pub/tex-archive
CTAN_AEGUILL=${CTAN_TEXARCH}/macros/latex/contrib/aeguill
CTAN_AE=${CTAN_TEXARCH}/fonts/ae/tex
CTAN_EUROSYM=${CTAN_TEXARCH}/fonts/eurosym/sty
CTAN_SETSPACE=${CTAN_TEXARCH}/macros/latex/contrib/setspace
CTAN_FNCYCHAP=${CTAN_TEXARCH}/macros/latex/contrib/fncychap
CTAN_FANCYHDR=${CTAN_TEXARCH}/macros/latex/contrib/fancyhdr
CTAN_LASTPAGE=${CTAN_TEXARCH}/macros/latex/contrib/lastpage
CTAN_ANYSIZE=${CTAN_TEXARCH}/macros/latex/contrib/anysize
CTAN_UCS=${CTAN_TEXARCH}/macros/latex/contrib/unicode
# misc: for ulem, hypernat
CTAN_MISC=${CTAN_TEXARCH}/macros/latex/contrib/misc
CTAN_PSFRAG=${CTAN_TEXARCH}/macros/latex/contrib/psfrag
CTAN_OBERDIEK=${CTAN_TEXARCH}/macros/latex/contrib/oberdiek
CTAN_MS=${CTAN_TEXARCH}/macros/latex/contrib/ms
CTAN_ROTATING=${CTAN_TEXARCH}/macros/latex/contrib/rotating
CTAN_SUBFIG=${CTAN_TEXARCH}/macros/latex/contrib/subfig
CTAN_CAPTION=${CTAN_TEXARCH}/macros/latex/contrib/caption
CTAN_MULTIROW=${CTAN_TEXARCH}/macros/latex/contrib/multirow

# create local directory for new packages, if necessary
if ! mkdir -p ${TEXMFLOCAL}
    then
    echo Error creating ${TEXMFLOCAL} directory
    echo Note that you need to run this script as root
    exit
fi

# define function to download and install all .sty, .fd files in a package
#
#    install_sty_pkg(pkg, url, localpath)
#
#       pkg: package name
#       url: CTAN URL of package
#       localpath: path to local repository of packages
install_sty_pkg() {
    PKG=${1}
    PKGDIR=${2}
    PKGLOCAL=${3}
    if [ -z `kpsewhich ${PKG}.sty` ] # package not installed yet
	then
	echo -n Dowloading from CTAN: ${PKG}...
	if wget -q -r -nd -A "*.sty,*.fd" ${PKGDIR}
	    then
	    echo " OK"
	else
	    echo " ERROR"
	    exit
	fi
	mkdir -p ${PKGLOCAL}/${PKG}
	mv *.sty *.fd ${PKGLOCAL}/${PKG}/ 2>/dev/null
    else
	echo Already installed: ${PKG}
    fi
}

# define function to download and install a single file from a package
#
#    install_file_pkg(file, pkg, url, localpath)
#
#       file: file name
#       pkg: package name
#       url: CTAN URL of package
#       localpath: path to local repository of packages
install_file_pkg() {
    FILE=${1}
    PKG=${2}
    PKGDIR=${3}
    PKGLOCAL=${4}
    if [ -z `kpsewhich ${FILE}` ]
	then
	echo -n Dowloading from CTAN: ${FILE} in ${PKG}...
	if wget -q ${PKGDIR}/${FILE}
	    then
	    echo " OK"
	else
	    echo " ERROR"
	    exit
	fi
	mkdir -p ${PKGLOCAL}/${PKG}
	mv ${FILE} ${PKGLOCAL}/${PKG}/ 2>/dev/null
    else
	echo Already installed: ${FILE} in ${PKG}
    fi
}

# download whole dir of files and copy to tetex subdirectory
#
#    install_dir_pkg(dir, pkg, url, localpath)
#
#       dir: dir name
#       pkg: package name
#       url: CTAN URL of package
#       localpath: path to local repository of packages
install_dir_pkg() {
    DIR=${1}
    PKG=${2}
    PKGDIR=${3}
    PKGLOCAL=${4}
    if [ -e ${PKGDIR}/${DIR} ]
	then 
	echo Already installed: ${DIR} in ${PKGDIR}
    else
	echo -n Dowloading from CTAN: directory ${DIR} in ${PKG}...
	mkdir -p $DIR
	if wget -q -P $DIR -nd -r $DIR ${PKGDIR}/${DIR}
	    then
	    echo " OK"
	else
	    echo " ERROR"
	    exit
	fi
    fi
    mkdir -p ${PKGLOCAL}/${PKG}
    mv ${DIR} ${PKGLOCAL}/${PKG}/ 2>/dev/null
}

# define function to download, compile and install an sty file from a
# pair of dtx/ins files. Note that all .ins and .dtx files in the
# package are downloaded, but only ${file}.ins is compiled
#
#    install_ins_pkg(file, pkg, url, localpath)
#
#       file: file name without extension
#       pkg: package name
#       url: CTAN URL of package
#       localpath: path to local repository of packages
install_ins_pkg() {
    FILE=${1}
    PKG=${2}
    PKGDIR=${3}
    PKGLOCAL=${4}
    if [ -z `kpsewhich ${FILE}.sty` ]
	then
	echo -n Dowloading from CTAN and creating: ${FILE}.sty in ${PKG}...
	if wget -q -r -nd -A "*.ins,*.dtx" ${PKGDIR}
	    then
	    echo " OK"
	else
	    echo " ERROR"
	    echo Cannot download ${FILE}.ins and ${FILE}.dtx
	    exit
	fi
	echo "... Compiling ${FILE}.sty from .ins and .dtx files"
	latex ${FILE}.ins > /dev/null
	mkdir -p ${PKGLOCAL}/${PKG}
	mv *.sty ${PKGLOCAL}/${PKG}/
	rm *
    else
	echo Already installed: ${FILE}.sty in ${PKG}
    fi
}

# define function to download and install from file dtx
#
#    install_dtx_pkg(file, pkg, url, localpath)
#
#       file: file name
#       pkg: package name
#       url: CTAN URL of package
#       localpath: path to local repository of packages
install_dtx_pkg() {
    FILE=${1}
    PKG=${2}
    PKGDIR=${3}
    PKGLOCAL=${4}
    if [ -z `kpsewhich ${FILE%.dtx}.sty` ]
	then
	echo -n Dowloading from CTAN: ${FILE} in ${PKG}...
	if wget -q ${PKGDIR}/${FILE}
	    then
	    echo " OK"
	else
	    echo " ERROR"
	    exit
	fi
	tex ${FILE} > /dev/null
	mkdir -p ${PKGLOCAL}/${PKG}
	mv *.sty ${PKGLOCAL}/${PKG}/
	rm *
    else
	echo Already installed: ${PKG}
    fi
}

# install packages
if [ -z `which apt-get` ]
    then
    echo
    echo Warning! apt-get not found: You need to install \"tetex-extra\"
    echo \(or any equivalent package that contains AMS-LaTeX\) yourself.
    echo Maybe your distribution uses a different package installer
    echo \(e.g. YaST2 in SuSE\). 
    echo
    echo Note that tetex-extra contains some of the packages that this
    echo script will install if they are not already in the system. So
    echo if you haven\'t installed tetex-extra yet, you should
    echo probably kill the script \(Ctrl + C\), install tetex-extra and
    echo run the script again.
    echo
    echo Otherwise, press ENTER to continue installing dependencies.
    read line
    else
    echo
    echo AMS-LaTeX comes bundled in tetex-extra:
    apt-get install tetex-extra # for amsmath
fi
install_sty_pkg aeguill ${CTAN_AEGUILL} ${PKGLOCAL}
install_sty_pkg ae ${CTAN_AE} ${PKGLOCAL}
install_sty_pkg eurosym ${CTAN_EUROSYM} ${PKGLOCAL}
install_sty_pkg setspace ${CTAN_SETSPACE} ${PKGLOCAL}
install_sty_pkg fncychap ${CTAN_FNCYCHAP} ${PKGLOCAL}
install_sty_pkg fancyhdr ${CTAN_FANCYHDR} ${PKGLOCAL}
install_sty_pkg anysize ${CTAN_ANYSIZE} ${PKGLOCAL}
install_file_pkg ulem.sty misc ${CTAN_MISC} ${PKGLOCAL}
install_ins_pkg lastpage lastpage ${CTAN_LASTPAGE} ${PKGLOCAL}
install_ins_pkg psfrag psfrag ${CTAN_PSFRAG} ${PKGLOCAL}
install_dtx_pkg pdflscape.dtx oberdiek ${CTAN_OBERDIEK} ${PKGLOCAL}
install_ins_pkg everyshi ms ${CTAN_MS} ${PKGLOCAL}
install_ins_pkg rotating rotating ${CTAN_ROTATING} ${PKGLOCAL}
install_ins_pkg subfig subfig ${CTAN_SUBFIG} ${PKGLOCAL}
install_ins_pkg caption caption ${CTAN_CAPTION} ${PKGLOCAL}
install_sty_pkg multirow ${CTAN_MULTIROW} ${PKGLOCAL}
install_file_pkg hypernat.sty misc ${CTAN_MISC} ${PKGLOCAL}
install_file_pkg ucs.sty ucs ${CTAN_UCS} ${PKGLOCAL}
install_file_pkg utf8x.def ucs ${CTAN_UCS} ${PKGLOCAL}
install_file_pkg ucsencs.def ucs ${CTAN_UCS} ${PKGLOCAL}
install_dir_pkg data ucs ${CTAN_UCS} ${PKGLOCAL}

# refresh package list
texhash ${TEXMFLOCAL}

# delete temp directory
popd > /dev/null
rm -rf $TMPDIR
